var models = require('../db');
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var $sql = require('../sqlMap');



// 连接数据库
var conn = mysql.createConnection(models.mysql);

conn.connect();
var jsonWrite = function(res, ret) {
  if(typeof ret === 'undefined') {
    res.json({
      code: '1',
      msg: '操作失败'
    });
  } else {
    res.json(ret);
  }
};

// 增加商品类型接口 --- 路径
router.post('/insertOrders', (req, res) => {
	//调用sql语句 --- sqlMap.js文件夹
  let sql = $sql.ordersdesc.insert;
  //接收前台数据
  let params = req.body;
  console.log(params);
  conn.query(sql, [params.oid, params.gid, params.number, params.money], function(err, result) {
    if (err) {
      console.log("添加失败"+err);
	  jsonWrite(res, result);
    }
    if (result) {
      jsonWrite(res, result);
    }
  });
});

// 查询某个订单详情
router.post('/selectOrdersOid', (req, res) => {
	//对应sqlMap.js文件中的sql语句
  let sql = $sql.ordersdesc.selectOrdersOid;
  let params = req.body;
  console.log(params);
  conn.query(sql,[params.oid] ,function(err, result) {
    if (err) {
      console.log("查询失败"+err);
    }
    if (result) {
      jsonWrite(res, result);
    }
  })
});

// 修改评价状态
router.post('/updateState', (req, res) => {
	//对应sqlMap.js文件中的sql语句
  let sql = $sql.ordersdesc.updateState;
  let params = req.body;
  console.log(params);
  conn.query(sql,[params.oid,params.gid] ,function(err, result) {
    if (err) {
      console.log("查询失败"+err);
    }
    if (result) {
      jsonWrite(res, result);
    }
  })
});


router.post('/selectTu', (req, res) => {
	//对应sqlMap.js文件中的sql语句
  let sql = $sql.ordersdesc.selectTu;

  conn.query(sql,function(err, result) {
    if (err) {
      console.log("查询失败"+err);
    }
    if (result) {
      jsonWrite(res, result);
    }
  })
});

module.exports = router;